import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class jdbcDemo2 {
    public static void main(String[] args) throws SQLException {
        //1.创建数据源(DataSource)
       DataSource dataSource =new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/117java?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");
        //2.和数据库服务器进行通信，需要建立网络链接
        Connection connection =dataSource.getConnection();
        //3.3.构造SQL语句
        String sql="select*from student";
       PreparedStatement preparedStatement = connection.prepareStatement(sql);
       //4.执行sql语句
      ResultSet resultSet = preparedStatement.executeQuery();
      while (resultSet.next()){
          int id=resultSet.getInt("id");
          String name=resultSet.getString("name");
          System.out.println(id+" "+name);
          //5.关闭连接等资源
          resultSet.close();
          preparedStatement.close();
          connection.close();
      }
    }
}
